const { ccclass, property } = cc._decorator;

/**
 * 音频管理类
 */
@ccclass
export default class EffectManager extends cc.Component {

    @property(cc.AudioSource)
    effectAudioSource: cc.AudioSource = null;

    @property([cc.AudioClip])
    audioClipArray: cc.AudioClip[] = []

    // 用于快速查找对应的audioClip
    private _audioClipMap: Map<string, cc.AudioClip> = new Map()

    start(): void {
        this.audioClipArray.forEach(audioClip => {
            this._audioClipMap.set(audioClip.name, audioClip)
        })
    }

    /**
     * 播放指定音乐
     * @param effectName 
     */
    public play(effectName: string) {
        if (!this._audioClipMap.has(effectName)) {
            throw new Error(`this effect name(${effectName}) does not exist.`)
        }

        // 获取音效clip
        const audioClip = this._audioClipMap.get(effectName)
        this.effectAudioSource.clip = audioClip
        // 播放
        this.effectAudioSource.play()
    }
}
